<?php
namespace backend\controller;
use backend\controller\BackendBaseController;
use jimu\Request;
use common\model\article_model;

class ArticleController extends BackendBaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function addpageAction()
    {
        return '大兄弟，功能还没实现';
    }

    public function indexAction()
    {
        load_class('paginator','',0);


        $epage = 10;
        $keyword = Request::get('keyword') ;
        $search_field = Request::get('search_field') ;
        $title = $article_id = '';
        $filter = ['cat_id'=>20];
        if($search_field){
            if($search_field == 'title'){
                $title      = $keyword;
                $filter['title'] = $title;
            }else{
                $article_id = $keyword;
                $filter['article_id'] =   $article_id;
            }
        }

        $page = Request::get('p') ;
        $page = $page ? $page-1 : 0;
        $offSet = $page * $epage;

        $model = new article_model();

        $count = $model->get_one($filter,'COUNT(article_id) AS total');

        $list = $model->getList(
            $filter,
            'article_id,title,create_time,status',
            $orderBy='sort_order DESC,article_id DESC',
            "{$offSet},{$epage}",
            ['date_format'=>'Y-m-d']
        );

         $this->assign('list',$list);

        $page_config = [
            'page' => 'p',
            'pagePattern' => '/([\/&?]p[\/=])([\d]+)/',
            'prevTheme' => '<a {href}><li class="{class}">上一页</li></a>',
            'nextTheme' => '<a {href}><li class="{class}">下一页</li></a>',
            'suffixTheme' => '<li class="totalPage">共<span>{totalPages}</span>页  当前第<span>{nowPage}</span>页</li>',
            'showTheme' => '<ul class="pageMenu clearfix">{getSuffixPage}{getFirstLinkPage}{getPrevLinkPage}{getLeftLinkPage}{getNowLinkPage}{getRightLinkPage}{getNextLinkPage}{getLastLinkPage}</ul>'
        ];
        $paginator = new \paginator($count['total'], $epage, null, $page_config);
        $pageBar =  $paginator->show();
         $this->assign('keyword',$keyword);
         $this->assign('pageBar',$pageBar);
         $this->display();
    }

    public function verifyAction()
    {
        debug_print($_REQUEST);
    }

    public function demoAction()
    {
        echo 'v';
    }

    public function editAction()
    {
        return '大兄弟，功能还没实现';
    }
    
    public function pageAction()
    {
        $this->display('page');
    }

}